package xin.marcher.module.lifecycle.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.utils.JsonUtil;
import xin.marcher.module.lifecycle.domain.dto.CategoryExpriWhiteDTO;
import xin.marcher.module.lifecycle.domain.request.CategoryExpriWhiteRequest;
import xin.marcher.module.lifecycle.domain.response.CategoryExpriWhiteResponse;
import xin.marcher.module.lifecycle.service.CategoryExpriWhiteService;

/**
 *
 */
@Slf4j
@RestController
@RequestMapping("/expriWhite")
public class CategoryExpriWhiteController {

    @Autowired
    private CategoryExpriWhiteService categoryExpriWhiteService;

    /**
     * 保存白名单
     *
     * @param request
     * @return
     */
    @RequestMapping("/saveExpriWhite")
    public JsonResult<CategoryExpriWhiteResponse> saveExpriWhite(CategoryExpriWhiteRequest request) {
        try {
            CategoryExpriWhiteResponse response = categoryExpriWhiteService.saveExpriWhite(request);
            return JsonResult.buildSuccess(response);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 修改白名单
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateExpriWhite")
    public JsonResult<CategoryExpriWhiteResponse> updateExpriWhite(CategoryExpriWhiteRequest request) {
        try {
            CategoryExpriWhiteResponse response = categoryExpriWhiteService.updateExpriWhite(request);
            return JsonResult.buildSuccess(response);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 获取品类考核白名单
     *
     * @param categoryId
     * @return
     */
    @RequestMapping("/getExpriWhite")
    public JsonResult<CategoryExpriWhiteDTO> getExpriWhite(Integer categoryId) {
        try {
            CategoryExpriWhiteDTO result = categoryExpriWhiteService.getExpriWhite(categoryId);
            return JsonResult.buildSuccess(result);
        } catch (Exception e) {
            log.error("system error: request={}", categoryId, e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 批量获取考核白名单
     *
     * @param request
     * @return
     */
    @RequestMapping("/pageExpriWhite")
    public JsonResult<PageResult<CategoryExpriWhiteDTO>> pageExpriWhite(CategoryExpriWhiteRequest request) {
        try {
            PageResult<CategoryExpriWhiteDTO> result = categoryExpriWhiteService.pageExpriWhite(request);
            return JsonResult.buildSuccess(result);
        } catch (Exception e) {
            log.error("system error: request={}", JsonUtil.object2Json(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

}
